package com.ocom.human.controller;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ocom.common.constant.HumanServiceUrl;
import com.ocom.common.entity.human.*;
import com.ocom.common.enums.ResultEnum;
import com.ocom.common.request.company.companyrole.SetPageByRoleRequest;
import com.ocom.common.resp.Result;
import com.ocom.common.utils.ResultUtil;
import com.ocom.human.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@Slf4j
@RestController
public class PermissionsController {

    @Autowired
    private PageEnumService pageEnumService;

    @Autowired
    private RolePermissionService rolePermissionService;

    @Autowired
    private ResourceApiService resourceApiService;

    @Autowired
    private ResourceMenuService resourceMenuService;

    @Autowired
    private SaasInfoService saasInfoService;

    /**
     *@Description:获取系统角色列表
     * @author lu
     * @return com.ocom.common.resp.Result
     * @date 2022/11/1 10:43       
     */
    @GetMapping(HumanServiceUrl.URL_getSysRoleList)
    public Result getSysRoleList(){
        List<EntityPageEnum> list=pageEnumService.selectPageEnum();
        Map<String,Object> map=new HashMap<>();
        List<Map<String,Object>> mapList=new ArrayList<>();
        for (EntityPageEnum entityPageEnum : list) {
            Map<String,Object> mapx=new HashMap<>();
            mapx.put("role",entityPageEnum.getTip());
            mapx.put("roleName",entityPageEnum.getVal());
            mapList.add(mapx);
        }
        map.put("roleList",mapList);
        return ResultUtil.success(map);
    }

    /**
     *@Description:获取角色网页权限
     * @author lu
     * @param role
     * @param comId
     * @param pageId
     * @return com.ocom.common.resp.Result
     * @date 2022/11/2 14:02
     */
    @PostMapping(HumanServiceUrl.URL_getRolePagePermission)
    public Result getRolePagePermission(Integer role,Long comId,Integer pageId){
        List<EntityRolePermission> list=rolePermissionService.selectRolePagePermission(comId,role,pageId);
        Map<String,Object> map=new HashMap<>();
        List<Map<String,Object>> mapList=new ArrayList<>();
        for (EntityRolePermission entityRolePermission : list) {
            Map<String,Object> mapx=new HashMap<>();
            mapx.put("role",entityRolePermission.getRole());
            mapx.put("pageId",entityRolePermission.getPageId());
            mapx.put("permission",entityRolePermission.getPermission());
            mapList.add(mapx);
        }
        map.put("permissionList",mapList);
        return ResultUtil.success(map);
    }

    /**
     *@Description:添加WEB页面（addWebPage）
     * @author lu 
     * @param pageCode
     * @param pageName
     * @param description
     * @return com.ocom.common.resp.Result
     * @date 2022/11/2 14:39       
     */
    @PostMapping(HumanServiceUrl.URL_addWebPage)
    public Result addWebPage(String pageCode,String pageName,String description,String pageAppCode,
        @RequestParam(required = false , defaultValue = "0") Integer parentId){
        // 如果parentId不为-1，默认为0；
        if (parentId!=-1){
            parentId = 0;
        }
        Integer page=resourceApiService.WebPage(pageCode,pageName,parentId);
        if (page==1){
            return ResultUtil.error(ResultEnum.Err_4044);
        }else if (page==2){
            return ResultUtil.error(ResultEnum.Err_4045);
        }

        EntityResourceApi entityResourceApi=new EntityResourceApi();
        entityResourceApi.setParentId(0);
        entityResourceApi.setApiCode(pageCode);
        entityResourceApi.setApiName(pageName);
        entityResourceApi.setDescription(description);
        Date date=new Date();
        entityResourceApi.setCreateTime(date);
        entityResourceApi.setUpdateTime(date);
        entityResourceApi.setStatus((byte) 1);
        entityResourceApi.setParentId(parentId);
        entityResourceApi.setPageAppCode(pageAppCode);
        Boolean b=resourceApiService.saveResourceApi(entityResourceApi);
        if (b){
            Map<String,Object> map=new HashMap<>();
            map.put("pageId",entityResourceApi.getId());
            return ResultUtil.success(map);
        }else {
            return ResultUtil.error(ResultEnum.Err_2016);
        }
    }

    @PostMapping(HumanServiceUrl.URL_modifyWebPage)
    public Result modifyWebPage(Integer pageId,String pageName,String description,String pageCode,String pageAppCode,
        @RequestParam(required = false,defaultValue = "0") Integer parentId){
        // 如果parentId不为-1，默认为0；
        if (parentId!=-1){
            parentId = 0;
        }
        return resourceApiService.modifyWebPage(pageId,pageName,description,pageCode,pageAppCode,parentId);
    }
    /**
     *@Description:4.8设置页面权限（setPagePermission）
     * @author lu 
     * @param jsonObject
     * @return com.ocom.common.resp.Result
     * @date 2022/11/2 15:46       
     */
    @PostMapping(HumanServiceUrl.URL_setPagePermission)
    public Result setPagePermission(@RequestBody JSONObject jsonObject){
        Boolean b=rolePermissionService.operationRolePagePermission(jsonObject);
        return ResultUtil.success();
    }

    /**
     *@Description:获取网页按钮列表
     * @author lu 
     * @param pageId
     * @return com.ocom.common.resp.Result
     * @date 2022/11/3 11:19       
     */
    @PostMapping(HumanServiceUrl.URL_getWebButtonList)
    public Result getWebButtonList(Integer pageId){
        List<EntityResourceApi> list=resourceApiService.selectWebButtonList(pageId);
        Map<String,Object> map=new HashMap<>();
        List<Map<String,Object>> mapList=new ArrayList<>();
        for (EntityResourceApi entityResourceApi : list) {
            Map<String,Object> mapx=new HashMap<>();
            mapx.put("buttonId",entityResourceApi.getId());
            mapx.put("buttonCode",entityResourceApi.getApiCode());
            mapx.put("buttonName",entityResourceApi.getApiName());
            mapx.put("description",entityResourceApi.getDescription());
            mapList.add(mapx);
        }
        map.put("buttonList",mapList);
        return ResultUtil.success(map);
    }

    /**
     *@Description:获取角色按钮权限（getRoleButtonPermission）
     * @author lu 
     * @param role
     * @param comId
     * @param pageId 
     * @return com.ocom.common.resp.Result
     * @date 2022/11/4 10:11       
     */
    @PostMapping(HumanServiceUrl.URL_getRoleButtonPermission)
    public Result getRoleButtonPermission(Integer role,Integer comId,Integer pageId){
        List<EntityRolePermission> list=rolePermissionService.getRoleButtonPermission(role,comId,pageId);
        Map<String,Object> map=new HashMap<>();
        List<Map<String,Object>> mapList=new ArrayList<>();
        for (EntityRolePermission entityRolePermission : list) {
            Map<String,Object> mapx=new HashMap<>();
            mapx.put("role",entityRolePermission.getRole());
            mapx.put("buttonId",entityRolePermission.getApiId());
            mapx.put("permission",entityRolePermission.getPermission());
            mapList.add(mapx);
        }
        map.put("permissionList",mapList);
        return ResultUtil.success(map);
    }

    /**
     *@Description:删除页面按钮（deleteWebButton）
     * @author lu 
     * @param pageId
     * @param buttonId 
     * @return com.ocom.common.resp.Result
     * @date 2022/11/4 14:03       
     */
    @PostMapping(HumanServiceUrl.URL_deleteWebButton)
    public Result deleteWebButton(Integer pageId,Integer buttonId){
       EntityResourceApi entityResourceApi=resourceApiService.selectWebButton(buttonId);
       if (entityResourceApi==null){
           return ResultUtil.error(ResultEnum.Err_1000);
       }
       if (entityResourceApi.getParentId().compareTo(pageId) != 0){
           return ResultUtil.error(ResultEnum.Err_4047);
       }
       Boolean b=resourceApiService.deleteWebButton(buttonId);
       if (b){
           Boolean c=rolePermissionService.deleteWebButton(pageId,buttonId);
           if (c){
               return ResultUtil.success();
           }
       }
       return ResultUtil.error(ResultEnum.Err_3016);
    }

    /**
     *@Description:添加WEB页面按钮（addWebButton）
     * @author lu 
     * @param pageId
     * @param buttonCode
     * @param buttonName
     * @param description
     * @return com.ocom.common.resp.Result
     * @date 2022/11/4 14:13       
     */
    @PostMapping(HumanServiceUrl.URL_addWebButton)
    public Result addWebButton(Integer pageId,String buttonCode,String buttonName,String description){
        return resourceApiService.getRoleButtonPermission(pageId,buttonCode,buttonName,description);
    }

    /**
     *@Description:修改WEB页面按钮（modifyWebButton）
     * @author lu 
     * @param pageId
     * @param buttonId
     * @param buttonName
     * @param description
     * @return com.ocom.common.resp.Result
     * @date 2022/11/4 14:40       
     */
    @PostMapping(HumanServiceUrl.URL_modifyWebButton)
    public Result modifyWebButton(Integer pageId,Integer buttonId,String buttonName,String description){
        return resourceApiService.modifyWebButton(pageId,buttonId,buttonName,description);
    }


    /**
     *@Description:获取消费版本列表（getXfVerList）
     * @author lu
     * @return com.ocom.common.resp.Result
     * @date 2022/11/7 9:11       
     */
    @PostMapping(HumanServiceUrl.URL_getXfVerList)
    public Result getXfVerList(){
        List<EntityPageEnum> list=pageEnumService.getXfVerList();
        Map<String,Object> map=new HashMap<>();
        List<Map<String,Object>> mapList=new ArrayList<>();
        Map<String,Object> maps=new HashMap<>();
        maps.put("ver",0);
        maps.put("verName","无消费");
        mapList.add(maps);
        for (EntityPageEnum entityPageEnum : list) {
            Map<String,Object> mapx=new HashMap<>();
            mapx.put("ver",Integer.valueOf(entityPageEnum.getTip()));
            mapx.put("verName",entityPageEnum.getVal());
            mapList.add(mapx);
        }
        map.put("xfVerList",mapList);
        return ResultUtil.success(map);
    }

    /**
     *@Description:取门禁版本列表
     * @author lu
     * @return com.ocom.common.resp.Result
     * @date 2022/11/7 9:33       
     */
    @PostMapping(HumanServiceUrl.URL_getMjVerList)
    public Result getMjVerList(){
        List<EntityPageEnum> list=pageEnumService.getMjVerList();
        Map<String,Object> map=new HashMap<>();
        List<Map<String,Object>> mapList=new ArrayList<>();
        Map<String,Object> maps=new HashMap<>();
        maps.put("ver",0);
        maps.put("verName","无门禁");
        mapList.add(maps);
        for (EntityPageEnum entityPageEnum : list) {
            Map<String,Object> mapx=new HashMap<>();
            mapx.put("ver",Integer.valueOf(entityPageEnum.getTip()));
            mapx.put("verName",entityPageEnum.getVal());
            mapList.add(mapx);
        }
        map.put("mjVerList",mapList);
        return ResultUtil.success(map);
    }

    /**
     *@Description:获取SaaS菜单列表
     * @author lu
     * @return com.ocom.common.resp.Result
     * @date 2022/11/7 14:54       
     */
    @PostMapping(HumanServiceUrl.URL_getSaasMenuList)
    public Result getSaasMenuList(Integer saasNo){
        List<EntityResourceMenu> list=resourceMenuService.getSaasMenuList(saasNo,0,0);
        if (list.size()<=0){
            EntitySaasInfo saasInfo= saasInfoService.getSaas(saasNo);
            if (saasInfo==null){
                return ResultUtil.error(ResultEnum.Err_1000);
            }else {
                if (resourceMenuService.InsertSaasMenu(saasNo,saasInfo.getSaasName())){
                    List<EntityResourceMenu> lists=resourceMenuService.getSaasMenuList(saasNo,0,0);
                    Map<String,Object> map=new HashMap<>();
                    List<Map<String,Object>> mapList=new ArrayList<>();
                    for (EntityResourceMenu entityResourceMenu : lists) {
                        Map<String,Object> mapx=new HashMap<>();
                        mapx.put("menuId",entityResourceMenu.getId());
                        mapx.put("parentId",entityResourceMenu.getParentId());
                        mapx.put("type",entityResourceMenu.getType());
                        mapx.put("sort",entityResourceMenu.getSort());
                        mapx.put("name",entityResourceMenu.getName());
                        mapx.put("icon",entityResourceMenu.getIcon());
                        mapx.put("description",entityResourceMenu.getDescription());
                        mapx.put("pageId",entityResourceMenu.getPageId());
                        mapx.put("xfVerList",entityResourceMenu.getXfVerList());
                        mapx.put("mjVerList",entityResourceMenu.getMjVerList());
                        mapx.put("appIcon",entityResourceMenu.getAppIcon());
                        mapx.put("ifCustom",entityResourceMenu.getIfCustom());
                        mapList.add(mapx);
                    }
                    map.put("menuList",mapList);
                    return ResultUtil.success(map);
                }else {
                    return ResultUtil.error(ResultEnum.Err_1000);
                }
            }
        }else {
            Map<String,Object> map=new HashMap<>();
            List<Map<String,Object>> mapList=new ArrayList<>();
            for (EntityResourceMenu entityResourceMenu : list) {
                Map<String,Object> mapx=new HashMap<>();
                mapx.put("menuId",entityResourceMenu.getId());
                mapx.put("parentId",entityResourceMenu.getParentId());
                mapx.put("type",entityResourceMenu.getType());
                mapx.put("sort",entityResourceMenu.getSort());
                mapx.put("name",entityResourceMenu.getName());
                mapx.put("icon",entityResourceMenu.getIcon());
                mapx.put("description",entityResourceMenu.getDescription());
                mapx.put("pageId",entityResourceMenu.getPageId());
                mapx.put("xfVerList",entityResourceMenu.getXfVerList());
                mapx.put("mjVerList",entityResourceMenu.getMjVerList());
                mapx.put("appIcon",entityResourceMenu.getAppIcon());
                mapx.put("ifCustom",entityResourceMenu.getIfCustom());
                mapList.add(mapx);
            }
            map.put("menuList",mapList);
            return ResultUtil.success(map);
        }
    }

    /**
     *@Description:删除Saas菜单（removeSaasMenu）
     * @author lu 
     * @param saasNo
     * @return com.ocom.common.resp.Result
     * @date 2022/11/8 13:32       
     */
    @PostMapping(HumanServiceUrl.URL_removeSaasMenu)
    public Result removeSaasMenu(Integer saasNo,Integer menuId){
        EntityResourceMenu entityResourceMenu = resourceMenuService.getSaasMenu(saasNo,menuId);
        if (entityResourceMenu !=null){
            return ResultUtil.error(ResultEnum.Err_4052);
        }

        EntityResourceMenu entityResourceMenus = resourceMenuService.getMenuId(menuId);
        if (entityResourceMenus == null){
            return ResultUtil.error(ResultEnum.Err_4053);
        }

        if (resourceMenuService.removeSaasMenu(menuId)){
            resourceMenuService.updateResourceMenus(entityResourceMenus);
            return ResultUtil.success();
        }else {
            return ResultUtil.error(ResultEnum.Err_3016);
        }
    }

    /**
     *@Description:上移Saas菜单
     * @author lu 
     * @param saasNo
     * @param menuId
     * @return com.ocom.common.resp.Result
     * @date 2022/11/8 15:52       
     */
    @PostMapping(HumanServiceUrl.URL_moveUpSaasMenu)
    public Result moveUpSaasMenu(Integer saasNo,Integer menuId){
        EntityResourceMenu entityResourceMenu = resourceMenuService.getMenuId(menuId);
        if (entityResourceMenu == null){
            return ResultUtil.error(ResultEnum.Err_4053);
        }

        EntityResourceMenu entityResourceMenus = resourceMenuService.getSaasMenus(entityResourceMenu);
        if (entityResourceMenus!=null){
            resourceMenuService.updateResourceMenu(entityResourceMenus,entityResourceMenu.getId());
        }
        return ResultUtil.success();
    }

    /**
     *@Description:下移Saas菜单
     * @author lu 
     * @param saasNo
     * @param menuId 
     * @return com.ocom.common.resp.Result
     * @date 2022/11/9 9:30       
     */
    @PostMapping(HumanServiceUrl.URL_moveDownSaasMenu)
    public Result moveDownSaasMenu(Integer saasNo,Integer menuId){
        EntityResourceMenu entityResourceMenu = resourceMenuService.getMenuId(menuId);
        if (entityResourceMenu == null){
            return ResultUtil.error(ResultEnum.Err_4053);
        }

        EntityResourceMenu entityResourceMenus = resourceMenuService.getGtSaasMenus(entityResourceMenu);
        if (entityResourceMenus!=null){
            resourceMenuService.updateMoveDownSaasMenu(entityResourceMenu,entityResourceMenus.getId());
        }
        return ResultUtil.success();
    }

    /**
     *@Description:添加Saas菜单
     * @author lu 
     * @param jsonObject 
     * @return com.ocom.common.resp.Result
     * @date 2022/11/9 10:30       
     */
    @PostMapping(HumanServiceUrl.URL_addSaasMenu)
    public Result addSaasMenu(@RequestBody JSONObject jsonObject){
        EntityResourceMenu parentMenu = resourceMenuService.parentMenu(jsonObject.getInteger("saasNo"),jsonObject.getInteger("parentMenuId"));
        if (parentMenu == null){
            return ResultUtil.error(ResultEnum.Err_4054);
        }

        JSONObject menuInfo=JSON.parseObject(JSON.toJSONString(jsonObject.get("menuInfo")),JSONObject.class);
        if (menuInfo.getInteger("type") ==0 || menuInfo.getInteger("type")<parentMenu.getType()){
            return ResultUtil.error(ResultEnum.Err_4055);
        }

        EntityResourceMenu entityResourceMenuNames = resourceMenuService.entityResourceMenuName(menuInfo.getString("name"),jsonObject.getInteger("parentMenuId"));
        if (entityResourceMenuNames !=null){
            return ResultUtil.error(ResultEnum.Err_4056);
        }

        EntityResourceMenu sort = resourceMenuService.sortResourceMenuName(menuInfo.getInteger("type"),jsonObject.getInteger("parentMenuId"));
        EntityResourceMenu entityResourceMenuName=new EntityResourceMenu();
        if (sort==null){
            entityResourceMenuName.setSort((byte) 1);
        }else {
            entityResourceMenuName.setSort((byte) (sort.getSort()+1));
        }
        entityResourceMenuName.setType(menuInfo.getByte("type"));
        entityResourceMenuName.setName(menuInfo.getString("name"));
        entityResourceMenuName.setIcon(menuInfo.getString("icon"));
        entityResourceMenuName.setDescription(menuInfo.getString("description"));
        entityResourceMenuName.setPageId(menuInfo.getInteger("pageId"));
        entityResourceMenuName.setXfVerList(menuInfo.getString("xfVerList"));
        entityResourceMenuName.setMjVerList(menuInfo.getString("mjVerList"));
        entityResourceMenuName.setSaasNo(jsonObject.getInteger("saasNo"));
        entityResourceMenuName.setAppIcon(menuInfo.getString("appIcon"));
        entityResourceMenuName.setIfCustom(menuInfo.getByte("ifCustom")==null?0:menuInfo.getByte("ifCustom"));
        Date date=new Date();
        entityResourceMenuName.setCreateTime(date);
        entityResourceMenuName.setUpdateTime(date);
        entityResourceMenuName.setParentId(jsonObject.getInteger("parentMenuId"));
        if(resourceMenuService.saveResourceMenuName(entityResourceMenuName)){
            Map<String,Object> map=new HashMap<>();
            map.put("menuId",entityResourceMenuName.getId());
            map.put("sort",entityResourceMenuName.getSort());
            return ResultUtil.success(map);
        }else {
            return ResultUtil.error(ResultEnum.Err_2016);
        }
    }

    /**
     *@Description:修改Saas菜单（modifySaasMenu）
     * @author lu 
     * @param jsonObject
     * @return com.ocom.common.resp.Result
     * @date 2022/11/9 14:54       
     */
    @PostMapping(HumanServiceUrl.URL_modifySaasMenu)
    public Result modifySaasMenu(@RequestBody JSONObject jsonObject){
        JSONObject menuInfo=JSON.parseObject(JSON.toJSONString(jsonObject.get("menuInfo")),JSONObject.class);
        EntityResourceMenu entityResourceMenuNames = resourceMenuService.entityResourceMenuNames(menuInfo.getString("name"),jsonObject.getInteger("parentMenuId"),jsonObject.getInteger("menuId"));
        if (entityResourceMenuNames !=null){
            return ResultUtil.error(ResultEnum.Err_4056);
        }
        if (resourceMenuService.updateResourceMenuName(jsonObject,menuInfo)){
            return ResultUtil.success();
        }else {
            return ResultUtil.error(ResultEnum.Err_3026);
        }
    }

    /**
     *@Description:获取SaaS列表（getSaasList）
     * @author lu
     * @return com.ocom.common.resp.Result
     * @date 2022/11/9 15:10       
     */
    @PostMapping(HumanServiceUrl.URL_getSaasList)
    public Result getSaasList(){
       List<EntitySaasInfo>  saasInfo= saasInfoService.getSaasList();
       Map<String,Object> map=new HashMap<>();
        map.put("saasList",saasInfo);
        return ResultUtil.success(map);
    }

    /**
     *@Description:获取角色SaaS菜单权限
     * @author lu 
     * @param jsonObject
     * @return com.ocom.common.resp.Result
     * @date 2022/11/10 14:16       
     */
    @PostMapping(HumanServiceUrl.URL_getSaasMenuByRole)
    public Result getSaasMenuByRole(@RequestBody JSONObject jsonObject){
        Map<String,Object> map=new HashMap<>();
        //List<EntityRolePermission> list=rolePermissionService.getRoleButtonPermission(jsonObject.getInteger("role"));
        List<EntityResourceApi> pageList=resourceApiService.getPageList(jsonObject.getInteger("role"), jsonObject.getInteger("webOrApp"));
        List<EntityResourceApi> buttoneList=resourceApiService.getButtoneList(jsonObject.getInteger("role"));
        List<EntityResourceMenu> menuList=resourceMenuService.getSaasMenuList(jsonObject.getInteger("saasNo"),jsonObject.getInteger("ifCustom"),jsonObject.getInteger("role"));
        if (menuList == null || menuList.size()<=0){
            List<EntityResourceMenu> list=new ArrayList<>();
            map.put("menuTree",list);
            return ResultUtil.success(map);
        }
        EntityResourceMenu entityResourceMenuOne=null;
        if (menuList.get(0).getType()!=0){
            List<EntityResourceMenu> list=new ArrayList<>();
            map.put("menuTree",list);
            return ResultUtil.success(list);
        }
        List<EntityResourceMenu> list=builTree(menuList,pageList,buttoneList,jsonObject);
        if (entityResourceMenuOne !=null){
            list.add(entityResourceMenuOne);
        }

        removeBuilTree(list);

        map.put("menuTree",list);
        return ResultUtil.success(map);
    }

    @PostMapping(HumanServiceUrl.URL_getCustomRolePower)
    public Result getCustomRolePower(@RequestBody JSONObject jsonObject){
        return ResultUtil.success(rolePermissionService.getCustomRolePower(jsonObject.getInteger("role"), jsonObject.getInteger("comId")));
    }


    public List<EntityResourceMenu> removeBuilTree(List<EntityResourceMenu> menuList) {
        for (EntityResourceMenu menuNode : getRootNode( menuList)) {
            removeTree(menuNode);
        }
        return menuList;
    }

    private EntityResourceMenu removeTree(EntityResourceMenu FuEntityResourceMenu){
        List<EntityResourceMenu> list=FuEntityResourceMenu.getChildMenu();
        List<EntityResourceMenu> treeMenus = new ArrayList<EntityResourceMenu>();
        for (EntityResourceMenu entityResourceMenu : list) {
            if (entityResourceMenu.getType()==1 || entityResourceMenu.getType() == 99 || entityResourceMenu.getType() == 100){
                if (entityResourceMenu.getChildMenu() !=null && entityResourceMenu.getChildMenu().size()>0){
                    treeMenus.add(entityResourceMenu);
                }
                if(ObjectUtil.isEmpty(entityResourceMenu.getChildMenu())){
                    removeTree(entityResourceMenu);
                }
            }else {
                treeMenus.add(entityResourceMenu);
            }
            if (entityResourceMenu.getChildMenu() !=null && entityResourceMenu.getChildMenu().size()>0){
                removeTree(entityResourceMenu);
            }
        }
        FuEntityResourceMenu.setChildMenu(treeMenus);
        return FuEntityResourceMenu;
    }


    //建立树形结构
    public List<EntityResourceMenu> builTree(List<EntityResourceMenu> menuList,List<EntityResourceApi> pageList,List<EntityResourceApi> buttoneList,JSONObject jsonObject) {
        List<EntityResourceMenu> treeMenus = new ArrayList<EntityResourceMenu>();
        List<EntityResourceMenu> rootMenu = getRootNode( menuList);
        for (EntityResourceMenu menuNode : rootMenu) {
            menuNode.setMenuId(menuNode.getId());
            menuNode = buildChilTree(menuNode,menuList,pageList,buttoneList,jsonObject);
            menuNode.setXfVerList(null);
            menuNode.setMjVerList(null);
           // menuNode.setDescription(null);
            menuNode.setUpdateTime(null);
            menuNode.setCreateTime(null);
            menuNode.setId(null);
            treeMenus.add(menuNode);
        }
        return treeMenus;
    }

    //递归，建立子树形结构
    private EntityResourceMenu buildChilTree(EntityResourceMenu resourceMenu,List<EntityResourceMenu> menuList,List<EntityResourceApi> pageList,List<EntityResourceApi> buttoneList,JSONObject jsonObject) {
        //子集合
        List<EntityResourceMenu> chilMenus = new ArrayList<EntityResourceMenu>();
        //
        Set<Integer> menuIds = new HashSet<>();
        for (EntityResourceMenu menuNode : menuList) {
            //
            if (menuNode.getParentId().equals(resourceMenu.getMenuId())) {
                //子级
                if (menuNode.getType()==2 || menuNode.getType()==3 || menuNode.getType()==101){
                    if (menuNode.getXfVerList()!=null){
                        String[] strXfVer = menuNode.getXfVerList().split(",");//根据，切分字符串
                        for (String s : strXfVer) {
                            if (s.equals(jsonObject.getString("xfVer"))){
                                if (menuNode.getType()==2 || menuNode.getType()==101){
                                    for (EntityResourceApi entityResourceApi : pageList) {
                                        if (menuNode.getPageId().compareTo(entityResourceApi.getId())==0){
                                            menuNode.setPageCode(entityResourceApi.getApiCode());
                                            menuNode.setPermission(entityResourceApi.getPermission());
                                            if (menuNode.getName() ==null){
                                                menuNode.setName(entityResourceApi.getApiName());
                                            }
                                            if (entityResourceApi.getPermission()==2){
                                                for (EntityResourceApi resourceApi : buttoneList) {
                                                    if (resourceApi.getPageId().compareTo(entityResourceApi.getId())==0){
                                                        menuNode.setPageButton(resourceApi.getApiCode());
                                                    }
                                                }
                                            }
                                            menuNode.setMenuId(menuNode.getId());
                                            if(!menuIds.contains(menuNode.getId())){
                                                chilMenus.add(buildChilTree(menuNode,menuList,pageList,buttoneList,jsonObject));
                                            }
                                            menuIds.add(menuNode.getId());
                                        }
                                    }
                                }else {
                                    menuNode.setMenuId(menuNode.getId());
                                    if(!menuIds.contains(menuNode.getId())) {
                                        chilMenus.add(buildChilTree(menuNode, menuList, pageList, buttoneList, jsonObject));
                                    }
                                    menuIds.add(menuNode.getId());
                                }
                            }
                        }
                    }

                    if (menuNode.getMjVerList() !=null){
                        String[] strMjVer = menuNode.getMjVerList().split(",");//根据，切分字符串
                        for (String s : strMjVer) {
                            if (s.equals(jsonObject.getString("mjVer"))){
                                if (menuNode.getType()==2 || menuNode.getType()==3 || menuNode.getType()==101){
                                    for (EntityResourceApi entityResourceApi : pageList) {
                                        if (menuNode.getPageId().compareTo(entityResourceApi.getId())==0){
                                            menuNode.setPageCode(entityResourceApi.getApiCode());
                                            menuNode.setPermission(entityResourceApi.getPermission());
                                            if (menuNode.getName() ==null){
                                                menuNode.setName(entityResourceApi.getApiName());
                                            }
                                            if (entityResourceApi.getPermission()==2){
                                                for (EntityResourceApi resourceApi : buttoneList) {
                                                    if (resourceApi.getPageId().compareTo(entityResourceApi.getId())==0){
                                                        menuNode.setPageButton(resourceApi.getApiCode());
                                                    }
                                                }
                                            }
                                            menuNode.setMenuId(menuNode.getId());
                                            chilMenus.add(buildChilTree(menuNode,menuList,pageList,buttoneList,jsonObject));
                                        }
                                    }
                                }else {
                                    menuNode.setMenuId(menuNode.getId());
                                    chilMenus.add(buildChilTree(menuNode,menuList,pageList,buttoneList,jsonObject));
                                }
                            }
                        }
                    }
                }else {
                    menuNode.setMenuId(menuNode.getId());
                    chilMenus.add(buildChilTree(menuNode,menuList,pageList,buttoneList,jsonObject));
                }
            }
        }

        resourceMenu.setChildMenu(chilMenus);
        resourceMenu.setId(null);
        resourceMenu.setXfVerList(null);
        resourceMenu.setMjVerList(null);
       // resourceMenu.setDescription(null);
        resourceMenu.setUpdateTime(null);
        resourceMenu.setCreateTime(null);
        return resourceMenu;
    }

    //获取根节点
    private List<EntityResourceMenu> getRootNode(List<EntityResourceMenu> menuList) {
        List<EntityResourceMenu> rootMenuLists = new ArrayList<EntityResourceMenu>();
        for (EntityResourceMenu menuNode : menuList) {
            if (menuNode.getParentId()==0) {
                rootMenuLists.add(menuNode);
            }
        }
        return rootMenuLists;
    }

    @PostMapping(HumanServiceUrl.URL_DeleteRolePermission)
    public Result deleteRolePermissionByRole(@RequestParam("role") Integer role){
        Boolean result = rolePermissionService.deleteRolePermissionByRole(role);
        return ResultUtil.success();
    }

    @PostMapping(HumanServiceUrl.URL_SetRolePermission)
    public Result setRolePermission(@RequestBody SetPageByRoleRequest request){
        Boolean result = rolePermissionService.addRolePermission(request);
        return result ? ResultUtil.success() : ResultUtil.error(ResultEnum.Err_2016);
    }

    /**
     *@Description:获取词典配置
     */
    @PostMapping(HumanServiceUrl.URL_getPageEnumByType)
    public Result getPageEnumByType(@RequestParam("type") String type){
        List<EntityPageEnum> list=pageEnumService.getPageEnumByType(type);

        Map<String,Object> map=new HashMap<>();
        /*
        List<Map<String,Object>> mapList=new ArrayList<>();
        Map<String,Object> maps=new HashMap<>();
        for (EntityPageEnum entityPageEnum : list) {
            Map<String,Object> mapx=new HashMap<>();
            mapx.put("type",entityPageEnum.getType());
            mapx.put("tip",entityPageEnum.getTip());
            mapx.put("val",entityPageEnum.getVal());
            mapx.put("sort",entityPageEnum.getSort());
            mapx.put("status",entityPageEnum.getStatus());
            mapList.add(mapx);
        }*/
        map.put("List",list);
        map.put("total",list.size());
        return ResultUtil.success(map);
    }
}
